package ishitong.vehicleManagement.service.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ishitong.common.Constants;
import ishitong.common.model.SysCode;
import ishitong.common.service.biz.SysCodeService;

/** @pdOid 04885692-abcf-46fd-afaa-8dde1a4a1be2 */
@Service(value = "spiSysCodeService")
@Transactional
public class SPISysCodeService {
   /** @pdOid afad62f2-66c4-48e0-8e26-e7b5d9b00045 */
   private static final String CODE_SLIDE = "#_#";
   /** @pdOid 3622bbd2-a6fe-4dc9-a5c8-a858ceab70ed */
   private List<SysCode> listSysCode;
   /** @pdOid 2f0a1632-fc00-42ff-970e-b12e8101f21b */
   private Map<String, SysCode> mapIdSysCode = new HashMap<String, SysCode>();
   /** @pdOid 46b932d8-738e-4dd6-af6a-c6208bd41506 */
   private Map<String, SysCode> mapCodeSysCode = new HashMap<String, SysCode>();
   /** @pdOid 0b01a1c5-45f9-422a-b5a7-265013380d43 */
   private Map<String, List<SysCode>> mapTypeSysCodeList = new HashMap<String, List<SysCode>>();
   
   /** @pdOid 620c3e68-1452-442f-91cb-6123287da426 */
   private void setMapByList() {
   	for (SysCode sysCode: listSysCode) {
   		mapIdSysCode.put(String.valueOf(sysCode.getId()), sysCode);
   		mapCodeSysCode.put(sysCode.getCode() + CODE_SLIDE + sysCode.getParentcode(), sysCode);
   		setMapTypeSysCodeList(sysCode);
   	}
   }
   
   /** @param sysCode
    * @pdOid b3023642-059b-41c9-8d3c-b787a8e9579f */
   private void setMapTypeSysCodeList(SysCode sysCode) {
   	String parentCode = sysCode.getParentcode();
   	if (mapTypeSysCodeList.get(parentCode) != null) {
   		mapTypeSysCodeList.get(parentCode).add(sysCode);
   	} else {
   		List<SysCode> lst = new ArrayList<SysCode>();
   		lst.add(sysCode);
   		mapTypeSysCodeList.put(parentCode, lst);
   	}
   }
   
   /** @pdOid d9f57d83-45a5-4833-9271-7a9e37238962 */
   @Autowired
   public SysCodeService sysCodeService;
   
   /** @pdOid 0df0ec79-e9e4-4d89-992c-d15a4962b435 */
   @Autowired
   public void init() {
   	this.listSysCode = sysCodeService.getSysCodeByCondition(new SysCode());
   	setMapByList();
   }
   
   /** @param id
    * @pdOid 6f25e044-01eb-43cd-b7ba-31f5aa3c8d32 */
   public SysCode getSysCodeById(String id) {
   	SysCode sc = mapIdSysCode.get(id);;
   	if (sc == null) {
   		sc = new SysCode();
   	}
   	return sc;
   }
   
   /** @param code 
    * @param parentcode
    * @pdOid 3426c935-176a-4ed0-92b6-05fccc578b7d */
   public SysCode getSysCodeByCodeAndParentcode(String code, String parentcode) {
   	SysCode sc = mapCodeSysCode.get(code + CODE_SLIDE + parentcode);
   	if (sc == null) {
   		sc = new SysCode();
   	}
   	return sc;
   }
   
   /** @param parentcode
    * @pdOid 6844a26d-120b-4bd8-adcd-b035ad35ce7a */
   public List<SysCode> getSysCodeListByTypeCode(String parentcode) {
   return mapTypeSysCodeList.get(parentcode);
    }
   
   /** @param parentcode 
    * @param sysCode
    * @pdOid 87de752c-d394-40ab-981f-dae8cb1a3223 */
   public List<SysCode> getSysCodeListByTypeCodeAndCondition(String parentcode, SysCode sysCode) {
   sysCode.setParentcode(parentcode);
      return sysCodeService.getSysCodeByCondition(sysCode);
    }
   
   /** @param ids
    * @pdOid 057ac326-b1a4-4d48-b2b4-17b46f46da78 */
   public List<SysCode> getSysCodeListByIds(String ids) {
   	List<SysCode> lstSysCode = new ArrayList<SysCode>();
   	if (ids != null && ids.length() > 0) {
   		String id[] = ids.split(Constants.SLIDE);
   		for (String strId: id) {
   			lstSysCode.add(this.getSysCodeById(strId));
   		}
   	}
   	return lstSysCode;
   }
   
   /** @param codes 
    * @param parentcode
    * @pdOid 5cbf61e8-a736-41a3-b6db-ca2815dfbe22 */
   public List<SysCode> getSysCodeListByCodesAndParentcode(String codes, String parentcode) {
   	List<SysCode> lstSysCode = new ArrayList<SysCode>();
   	if (codes != null && codes.length() > 0) {
   		String code[] = codes.split(Constants.SLIDE);
   		for (String strCode: code) {
   			lstSysCode.add(this.getSysCodeByCodeAndParentcode(strCode ,parentcode));
   		}
   	}
   	return lstSysCode;
   }

}